<template>
  <div>
    <el-card>
      <template #header>
        <span class="title">模板总数 {{ createdCount }}，使用次数 {{ useCount }}</span>
      </template>
      <el-row>
        <v-chart style="height: 500px" :option="barOption" :theme="isDark ? 'dark' : ''" autoresize />
      </el-row>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { getCount, getMonthlyCount, MonthlyDataProps } from '@/service/template'
import { useGlobalStore } from '@/stores/global'
// import { ECharts } from 'echarts'

const global = useGlobalStore()
const isDark = computed(() => global.isDark)
const createdCount = ref(0)
const useCount = ref(0)
const barOption = ref()

onMounted(async () => {
  const { count, use } = await getCount()
  createdCount.value = count
  useCount.value = use

  const rawData = await getMonthlyCount()
  barOption.value = parseMonthlyOpt(rawData)
})

// 默认 一年的时间范围
function parseMonthlyOpt(data: Array<MonthlyDataProps>) {
  const arr = data.map(item => {
    const { month, data: monthData } = item
    return [month, monthData.count, monthData.use]
  })

  return {
    title: {
      text: '每月模板创建',
      top: 10,
      left: 'center'
    },
    legend: {
      top: '10%'
    },
    tooltip: {},
    grid: {
      top: 140,
      bottom: 40
    },
    dataset: {
      source: [['月', '发布', '被使用'], ...arr]
    },
    xAxis: { type: 'category' },
    yAxis: {},
    series: [{ type: 'bar' }, { type: 'bar' }]
  }
}
</script>
<style lang="scss" scoped></style>
